package com.james.domain;

import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;

/**
  * @ClassName: User
  * @Author: JamesWang
  * @Date: 2021/4/18 16:00
  * @Description:  
  * @Version:1.0
 */
@Data
@AllArgsConstructor
@NoArgsConstructor
@TableName(value = "sy_user")
public class User implements Serializable, UserDetails {
    /**
     * 用户id
     */
    @TableId(value = "user_id", type = IdType.INPUT)
    private Integer userId;

    /**
     * 用户名
     */
    @TableField(value = "user_name")
    private String userName;

    /**
     * 用户密码
     */
    @TableField(value = "user_pwd")
    private String userPwd;

    /**
     * 用户头像图片
     */
    @TableField(value = "user_pic")
    private String userPic;

    /**
     * 用户积分
     */
    @TableField(value = "score")
    private Integer score;

    /**
     * 注册时间
     */
    @TableField(value = "create_time")
    private Date createTime;

    /**
     * 逻辑删除（0:删除，1:正常）
     */
    @TableField(value = "del_flag")
    private Integer delFlag;

    private static final long serialVersionUID = 1L;

    public static final String COL_USER_ID = "user_id";

    public static final String COL_USER_NAME = "user_name";

    public static final String COL_USER_PWD = "user_pwd";

    public static final String COL_USER_PIC = "user_pic";

    public static final String COL_SCORE = "score";

    public static final String COL_CREATE_TIME = "create_time";

    public static final String COL_DEL_FLAG = "del_flag";

    @Override
    public Collection<? extends GrantedAuthority> getAuthorities() {
        ArrayList<SimpleGrantedAuthority> simpleGrantedAuthorities = new ArrayList<>(2);
        SimpleGrantedAuthority admin = new SimpleGrantedAuthority("admin");
        simpleGrantedAuthorities.add(admin);
        return simpleGrantedAuthorities;
    }

    @Override
    public String getPassword() {
        return this.userPwd;
    }

    @Override
    public String getUsername() {
        return this.userId.toString();
    }

    @Override
    public boolean isAccountNonExpired() {
        return this.delFlag == 1;
    }

    @Override
    public boolean isAccountNonLocked() {
        return this.delFlag == 1;
    }

    @Override
    public boolean isCredentialsNonExpired() {
        return this.delFlag == 1;
    }

    @Override
    public boolean isEnabled() {
        return this.delFlag == 1;
    }
}